package com.doudou.casebook.frag_record;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import android.annotation.SuppressLint;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.doudou.casebook.R;
import com.doudou.casebook.db.AccountBean;
import com.doudou.casebook.db.TypeBean;
import com.doudou.casebook.utils.BeiZhuDialog;
import com.doudou.casebook.utils.KeyBoardUtils;
import com.doudou.casebook.utils.SelectTimeDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 记录页面的支出模块
 */
public abstract class BaseRecordFragment extends Fragment implements View.OnClickListener {
    KeyboardView keyboardView;
    EditText moneyEt;
    ImageView typeIv;
    List<TypeBean> typeList;
    TextView typeTv, beizhuTv, timeTv;
    GridView typeGv;
    TypeBaseAdapter adapter;
    AccountBean accountBean;//将要插入到记账本当中的数据保存成对象形式

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountBean = new AccountBean();
//        accountBean.setTypename("其他");
//        accountBean.setsImageId(R.mipmap.ic_qita_fs);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_out_come, container, false);
        initView(view);
        setInitTime();
        //给GridView填充数据
        loadDataToGV();
        //设置GridView每一项的点击事件
        setGVClickListener();
        return view;
    }

    //获取当前时间显示在time_tv上
    private void setInitTime() {
        Date date = new Date();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time = dateFormat.format(date);
        timeTv.setText(time);
        accountBean.setTime(time);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        accountBean.setYear(year);
        accountBean.setMonth(month);
        accountBean.setDay(day);
    }

    //设置GridView每一项的点击事件
    private void setGVClickListener() {
        typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectPos = position;
                adapter.notifyDataSetChanged();//提示绘制发生变化
                TypeBean typeBean = typeList.get(position);
                String typeName = typeBean.getTypeName();
                typeTv.setText(typeName);
                int sImageId = typeBean.getsImageId();
                typeIv.setImageResource(sImageId);

                //实时更新accountBean的数据
                accountBean.setTypename(typeName);
                accountBean.setsImageId(sImageId);
            }
        });
    }

    public void loadDataToGV() {
        typeList = new ArrayList<>();
        adapter = new TypeBaseAdapter(getContext(), typeList);
        typeGv.setAdapter(adapter);

    }

    //初始化Fragment里面的数据
    private void initView(View view) {
        keyboardView = view.findViewById(R.id.frag_record_keyboard);
        moneyEt = view.findViewById(R.id.frag_record_et_money);
        typeIv = view.findViewById(R.id.frag_record_iv);
        typeTv = view.findViewById(R.id.frag_record_tv_type);
        beizhuTv = view.findViewById(R.id.frag_record_tv_beizhu);
        timeTv = view.findViewById(R.id.frag_record_tv_time);
        typeGv = view.findViewById(R.id.frag_record_gv);

        beizhuTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);

        //让自定义软键盘显示
        KeyBoardUtils keyBoardUtils = new KeyBoardUtils(keyboardView, moneyEt);
        keyBoardUtils.showKeyboard();


        //设置接口监听确定按钮被点击了
        keyBoardUtils.setHuidiaojiekou(new KeyBoardUtils.Huidiaojiekou() {
            @Override
            public void huidiaojiekoufangfa() {

                // 获取输入的钱数
                String moneyStr = moneyEt.getText().toString();
                if (TextUtils.isEmpty(moneyStr) || moneyStr.equals("0")) {
                    requireActivity().finish();
                    return;
                }
                float money = Float.parseFloat(moneyStr);
                accountBean.setMoney(money);
                //获取记录的信息,存入数据库
                saveAccountToDB();
                //返回上一级页面
                requireActivity().finish();
                Toast.makeText(getContext(), "添加成功!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //让子类一定要重写这个方法
    public abstract void saveAccountToDB();

    @Override
    public void onClick(View v) {
        if (beizhuTv.getId() == v.getId()) {
            showBZDialog();
        } else if (timeTv.getId() == v.getId()) {
            showTimeDialog();
        }
    }

    //弹出时间选择对话框
    private void showTimeDialog() {
        SelectTimeDialog dialog = new SelectTimeDialog(requireContext());
        dialog.show();
        dialog.setDialogBtnOnClickListener(new SelectTimeDialog.DialogBtnOnClickListener() {
            @Override
            public void onEnsure(String time,int year,int month,int day) {

                timeTv.setText(time);
                accountBean.setTime(time);
                accountBean.setYear(Integer.parseInt(String.valueOf(year)));
                accountBean.setMonth(Integer.parseInt(String.valueOf(month)));
                accountBean.setDay(Integer.parseInt(String.valueOf(day)));
            }


        });
    }

    //弹出备注对话框
    public void showBZDialog() {
        BeiZhuDialog dialog = new BeiZhuDialog(requireContext());
        dialog.show();
        dialog.setDialogSize();
        dialog.setOnEnsureListener(new BeiZhuDialog.OnEnsureListener() {
            @Override
            public void OnEnsure() {
                String msg = dialog.getEditText();
                if (!TextUtils.isEmpty(msg)) {
                    beizhuTv.setText(msg);
                    accountBean.setBeizhu(msg);
                }
                dialog.cancel();
            }
        });

    }

}